import 'package:flutter/material.dart';
import 'package:flutter_basic_learning/utils/CommonWidget.dart';

class ListItemBean {
  String text;
  String imageUrl;
  ListItemBean(this.text, this.imageUrl);
}

class ListWidget extends StatefulWidget {
  const ListWidget({super.key});

  @override
  State<ListWidget> createState() => _ListWidgetState();
}

class _ListWidgetState extends State<ListWidget> {
  List<ListItemBean> data = [
    ListItemBean('我的第 1 条数据', 'images/panda.png'),
    ListItemBean('我的第 2 条数据', 'images/panda.png'),
    ListItemBean('我的第 3 条数据', 'images/panda.png'),
    ListItemBean('我的第 4 条数据', 'images/panda.png'),
  ];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Widget listItem(String text, String imageUrl) {
    Widget body = customRow(
      double.infinity,
      50,
      [
        Image.asset(imageUrl, width: 40, height: 40, fit: BoxFit.cover),
        horizontalSpacing(10),
        Text(
          text,
          style: const TextStyle(fontSize: 20),
        ),
      ],
    );
    return body;
  }

  Widget renderList() {
    Widget body = ListView.builder(
        shrinkWrap: true, // 重要：让 ListView 适应内容大小
        // physics: const NeverScrollableScrollPhysics(),
        itemCount: data.length,
        itemBuilder: (context, index) {
          return listItem(data[index].text, data[index].imageUrl);
        });
    // body = SizedBox(height: 300,width: double.infinity,child: body);
    return body;
  }

  @override
  Widget build(BuildContext context) {
    return commonScaffold('列表组件学习', [verticalSpacing(20), renderList()]);
  }
}
